package com.mojo.io.stream;

import java.io.*;

/**
 * 文件复制
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
public class FileCopy {
    public static void main(String[] args) {
        //读取文件
        //copy();
        copy1();
        // copy2();
        //输出文件
        // listAllFiles(new File("F:\\个人\\电子书"), "");
    }

    private static void copy2() {
        long s = System.currentTimeMillis();
        //BufferedInputStream 183毫秒
        //
        File inFile = new File("h:\\迅雷下载\\Apifox-windows-latest.zip");
        try (BufferedReader inputStream = new BufferedReader(new FileReader(inFile));
             BufferedWriter outputStream = new BufferedWriter(new FileWriter("f:\\Apifox\\"))) {
            char[] bytes = new char[8096*4];
            int str = 0;
            while ((str = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - s) + "毫秒");
    }

    private static void copy1() {
        long s = System.currentTimeMillis();
        //BufferedInputStream 183毫秒
        //
        File inFile = new File("h:\\迅雷下载\\Apifox-windows-latest.zip");
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(inFile));
             BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("f:\\Apifox\\"))) {
            byte[] bytes = new byte[8096*4];
            int n = 0;
            while ((n = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, n);
                //outputStream.write(n);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - s) + "毫秒");
    }

    private static void copy() {
        File inFile = new File("e:\\a.txt");
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(inFile));
             BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("d:\\a.txt"))) {
            byte[] bytes = new byte[8 * 1024];
            int n = 0;
            while ((n = inputStream.read(bytes)) != -1) {
                System.out.println(n);
                outputStream.write(bytes, 0, n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void listAllFiles(File dir, String c) {
        if (dir == null || !dir.exists()) {
            return;
        }
        if (dir.isFile()) {
            System.out.println(c + dir.getName());
            return;
        }
        String temp = c + "-";
        for (File file : dir.listFiles()) {
            listAllFiles(file, temp);
        }
    }
}
